#!/bin/bash

# ElasticSearch + Kibana 状态检查脚本
echo "🔍 检查 ElasticSearch + Kibana 服务状态..."

# 检查容器状态
echo "📦 容器状态:"
docker-compose ps

echo ""

# 检查 ElasticSearch
echo "🔍 ElasticSearch 状态检查..."
if curl -s http://localhost:9200 > /dev/null; then
    echo "✅ ElasticSearch 运行正常 (http://localhost:9200)"
    
    echo "📊 集群健康状态:"
    curl -s http://localhost:9200/_cluster/health?pretty | grep -E '"cluster_name"|"status"|"number_of_nodes"|"active_primary_shards"|"active_shards"'
    
    echo ""
    echo "📈 节点信息:"
    curl -s http://localhost:9200/_nodes/stats?pretty | grep -A 5 '"name"'
    
else
    echo "❌ ElasticSearch 无法访问 (http://localhost:9200)"
fi

echo ""

# 检查 Kibana
echo "🔍 Kibana 状态检查..."
if curl -s http://localhost:5601/api/status > /dev/null; then
    echo "✅ Kibana 运行正常 (http://localhost:5601)"
    
    echo "📊 Kibana 状态:"
    curl -s http://localhost:5601/api/status | python3 -m json.tool 2>/dev/null | grep -E '"overall"|"level"' || echo "状态信息获取中..."
    
else
    echo "❌ Kibana 无法访问 (http://localhost:5601)"
fi

echo ""

# 检查日志（最近50行）
echo "📝 最近日志 (最后50行):"
echo "--- ElasticSearch 日志 ---"
docker-compose logs --tail=25 elasticsearch

echo ""
echo "--- Kibana 日志 ---"
docker-compose logs --tail=25 kibana

echo ""
echo "🔧 管理命令:"
echo "   启动服务:     ./start.sh"
echo "   停止服务:     ./stop.sh"
echo "   查看日志:     docker-compose logs -f"
echo "   重启服务:     docker-compose restart"
